package com.jml.zlhq.app.ui;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.jml.zlhq.app.BuildConfig;
import com.jml.zlhq.app.R;
import com.jml.zlhq.app.URLs;
import com.jml.zlhq.app.bean.UserEntry;
import com.jml.zlhq.app.service.HttpService;
import com.jml.zlhq.app.util.StringUtil;
import com.jml.zlhq.app.util.UIUtil;
import com.loopj.android.http.TextHttpResponseHandler;

public class PinglunActivity extends BaseActivity {
	private int type=2;
	private RatingBar ratingBar;
	private View line;
	private EditText input;
	private LinearLayout top;
	private TextView msg;
	private String id;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pinglun_activity);
		
		LinearLayout leftLay = (LinearLayout) findViewById(R.id.content_header_left_lay);
		LinearLayout rightLay = (LinearLayout) findViewById(R.id.content_header_right_lay);
		leftLay.setOnClickListener(this);
		rightLay.setOnClickListener(this);
		AutoCompleteTextView centerEdit = (AutoCompleteTextView) findViewById(R.id.content_header_center_edit);
		centerEdit.setVisibility(View.GONE);
		ImageView leftImage = (ImageView) findViewById(R.id.content_header_left_img);
		leftImage.setVisibility(View.GONE);
		ImageView roghtImage1 = (ImageView) findViewById(R.id.content_header_right_img);
		roghtImage1.setVisibility(View.GONE);
		TextView centerText = (TextView) findViewById(R.id.content_header_center_text);
		centerText.setText("评论");
		TextView rightText = (TextView) findViewById(R.id.content_header_right_text);
		rightText.setText("发送");
		TextView cityChosed = (TextView) findViewById(R.id.content_header_center_text2);
		cityChosed.setVisibility(View.GONE);
		TextView leftText = (TextView) findViewById(R.id.content_header_left_text);
		leftText.setText("取消");
		
		ratingBar = (RatingBar) findViewById(R.id.pinglun_ratingbar);
		line = findViewById(R.id.pinglun_line);
		input = (EditText) findViewById(R.id.pinglun_input);
		top = (LinearLayout) findViewById(R.id.pinglun_top);
		msg = (TextView) findViewById(R.id.pinglun_msg);
		
		type = getIntent().getExtras().getInt("type");
		id = getIntent().getExtras().getString("id");
		
		if(!appContext.isLogin()){
			UIUtil.openActivity(this, LoginActivity.class, null, 5);
		}
		
		if(type==1){
			top.setVisibility(View.GONE);
			line.setVisibility(View.GONE);
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==5){
			if(!appContext.isLogin()){
				UIUtil.ToastMessage(activity, "用户未登录");
				finish();
			}
		}
	}

	@Override
	public void loadData(Map<String, String> param) {}

	@Override
	public void clearData() {}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.content_header_left_lay:
			finish();
			break;
		case R.id.content_header_right_lay:
			final String str = input.getText().toString();
			final int score = (int)ratingBar.getRating();
			if(StringUtil.isEmpty(str)){
				msg.setText("评论内容为空");
				return;
			}
			Map<String, String> map = new HashMap<String, String>();
			map.put("target_id", id);
			map.put("text", str);
			map.put("auth_token", appContext.getLoginUid());
			map.put("score", String.valueOf(score));
			final Dialog  mProDialog =  UIUtil.showLoadingDialog(activity, "提交中，请稍后...", false);
			HttpService.postByText(type==1?URLs.COMMENT_WORK:URLs.COMMENT_TEAM, map, new TextHttpResponseHandler() {
				
				@Override
				public void onSuccess(int arg0, Header[] arg1, String arg2) {
					mProDialog.dismiss();
					try {
						if(BuildConfig.DEBUG)Log.i("===result====", arg2);
						JSONObject result = new JSONObject(arg2);
						if(result.getInt("status")==0){
							UIUtil.ToastMessage(activity, "评论成功");
							JSONObject json = new JSONObject();
							JSONObject userJson = new JSONObject();
							JSONObject contentJson = new JSONObject();
							UserEntry user = new UserEntry();//appContext.getLoginUser();
							userJson.put("name", user.getUserName());
							userJson.put("logo_url", user.getUserImage());
							contentJson.put("text", str);
							contentJson.put("create_at", "刚刚");
							contentJson.put("score", score);
							json.put("user", userJson);
							json.put("content", contentJson);
							Intent intent = new Intent();
							intent.putExtra("id", id);
							intent.putExtra("comment", json.toString());
							setResult(Activity.RESULT_OK, intent);
							finish();
						}else{
							msg.setText(result.getString("message"));
						}
					} catch (Exception e) {
						e.printStackTrace();
						UIUtil.ToastMessage(activity, "评论失败");
					}
				}
				
				@Override
				public void onFailure(int arg0, Header[] arg1, String arg2, Throwable arg3) {
					mProDialog.dismiss();
					UIUtil.ToastMessage(activity, "评论失败");
					finish();
				}
			});
			break;
		default:
			break;
		}
	}

}
